#!/bin/bash
#
# Listening to Internet Radio using Mplayer
#
# Author       : Yu-Jie Lin
# Creation Date: 2007-11-19T17:48:09+0800

echo Internet Radio Stations:

stations=(\
'BBC Radio 1 (mms) UK only' \
'mplayer -playlist http://www.bbc.co.uk/radio1/wm_asx/aod/radio1.asx' \
'BBC Radio 1 (rtsp)' \
'mplayer -playlist http://www.bbc.co.uk/radio1/realaudio/media/r1live.ram' \
'BBC Radio 2 (rtsp)' \
'mplayer -playlist http://www.bbc.co.uk/radio2/realmedia/fmg2.ram' \
'BBC 4 FM (mms)' \
'mplayer -playlist http://www.bbc.co.uk/radio/listen/live/r4.asx' \
'BBC 4 FM (rtsp)' \
'mplayer -playlist http://www.bbc.co.uk/radio4/realplayer/media/fmg2.ram' \
'BBC Five Live (mms)' \
'mplayer -playlist http://www.bbc.co.uk/fivelive/live/live_int.asx' \
'BBC Five Live (rtsp)' \
'mplayer -playlist http://www.bbc.co.uk/fivelive/live/surestream_int.ram' \
'BBC 6 Music (mms)' \
'mplayer -playlist http://www.bbc.co.uk/6music/ram/6music.asx' \
'BBC 6 Music (rtsp)' \
'mplayer -playlist http://www.bbc.co.uk/6music/ram/dsatg2.ram' \
'BBC 7 (mms)' \
'mplayer -playlist http://www.bbc.co.uk/bbc7/realplayer/bbc7.asx' \
'BBC 7 (rtsp)' \
'mplayer -playlist http://www.bbc.co.uk/bbc7/realplayer/dsatg2.ram' \
'BBC World Service (mms)' \
'mplayer -playlist http://www.bbc.co.uk/worldservice/meta/tx/nb/live_news_au_nb.asx' \
'BBC World Service (rtsp)' \
'mplayer -playlist http://www.bbc.co.uk/worldservice/meta/tx/nb/live_news_au_nb.ram' \
'CNN Radio (mms)' \
'mplayer -playlist http://edition.cnn.com/audio/radio/liveaudio.asx' \
'NPR (mp3)' \
'mplayer -playlist http://www.shoutcast.com/sbin/shoutcast-playlist.pls?rn=5560&file=filename.pls' \
'NPR (mms)' \
'mplayer -playlist http://www.npr.org/templates/dmg/dmg.php?getProgramStream=true&NPRMediaPref=WM' \
'NPR (rtsp)' \
'mplayer -playlist http://www.npr.org/templates/dmg/dmg.php?getProgramStream=true&NPRMediaPref=RM' \
'WJBQ' \
'mplayer mms://citadelcc-wjbq-fm.wm.llnwd.net/citadelcc_WJBQ_FM?MSWMExt=.asf' \
)

function sigint {
	kill $p_pid
	# Show cursor
	echo -e "\033[?25h"
	# Echo for stdin
	stty echo
	exit 0
	}

function sigwinch {
	# Get host name
	raw=$(wget -q http://wjbq.com/ -O -)
	hostname=$(grep "hostName" <<< "$raw" | head -1 | sed -e 's/^[^<]*//' -e 's/<[^>]*>//g')
	hosttime=$(grep "hostTime" <<< "$raw" | head -1 | sed -e 's/^[^<]*//' -e 's/<[^>]*>//g')
	clear
	echo -ne "\033[1;35m"
	echo -n "WJBQ" | figlet -t -c -f 5lineoblique
	echo -ne "\033[0;39m\n\033[1;31m"
	echo -n "$title" | figlet -t -c -f cosmic
	echo -ne "\033[0;39m\n\033[1;32m"
	echo -n "$artist" | figlet -t -c -f cosmic
	echo -ne "\033[0;39m\n\033[1;34m"
	echo -n "$hostname" | figlet -t -c -f script
	echo -n "$hosttime" | figlet -t -c -f script
	echo -ne "\033[0;39m\n"
	(( space_length=($(stty size | cut -f 2 -d ' ') - 16) / 2 ))
	echo -ne "\033[${space_length}G"
	echo -ne "\033[1;35mhttp://wjbq.com/\033[0;39m"
	_title=$title
	_artist=$artist
	}

let "count = ${#stations[@]} / 2"

# List stations
index=0

while [[ $index -lt $count ]]; do
	# List all the elements in the array.
	(( i=$index * 2 ))
	echo "$index: ${stations[$i]}"
    (( index=$index + 1 ))
done

read -p "Which station? " radio
[[ "$radio" == "" ]] && exit 1
(( i=$radio * 2 + 1 ))

if [[ "${stations[$((i - 1))]}" == "WJBQ" ]]; then
	${stations[$i]} &> /dev/null &
	p_pid=$!
	# No echo for stdin
	stty -echo
	# Hide cursor
	echo -ne "\033[?25l"
	trap sigint INT EXIT
	trap sigwinch WINCH
	while true; do
		raw=$(wget -q http://ipex.citcomm.com/NowPlaying/WJBQFM.xml -O - | sed -e 's/<[^>]*>/|/g' -e 's/||*/|/g')
		# |Bad Girlfriend|Theory Of A Deadman|03:03|5/25/2009 8:21:34 AM|
		# TODO do timezome conversion? "%m/%d/%Y %l:%M:%S %p"
		artist=$(cut -d \| -f 3 <<< $raw)
		title=$(cut -d \| -f 2 <<< $raw)
		[[ "$title" != "$_title" || "$artist" != "$_artist" ]] && sigwinch
		read -n 1 -t 15 ch
		[[ $? -gt 0 ]] && continue
		case "$ch" in
			q)	break ;;
		esac
	done
else
	${stations[$i]}
fi
